home *** CD-ROM | disk | FTP | other *** search
/ Over 1,000 Windows 95 Programs / Over 1000 Windows 95 Programs (Microforum) (Disc 1).iso / 1258 / samples / filecopy.jav next >
Text File  |  1996-02-16  |  5KB  |  108 lines

  1. // This example is from the book _Java in a Nutshell_ by David Flanagan.
  2. // Written by David Flanagan.  Copyright (c) 1996 O'Reilly & Associates.
  3. // You may study, use, modify, and distribute this example for any purpose.
  4. // This example is provided WITHOUT WARRANTY either expressed or implied.
  5.  
  6. import java.io.*;
  7.  
  8. public class FileCopy {
  9.     public static void copy(String source_name, String dest_name) 
  10.         throws IOException
  11.     {
  12.         File source_file = new File(source_name);
  13.         File destination_file = new File(dest_name);
  14.         FileInputStream source = null;
  15.         FileOutputStream destination = null;
  16.         byte[] buffer;
  17.         int bytes_read;
  18.         
  19.         try {
  20.             // First make sure the specified source file 
  21.             // exists, is a file, and is readable.
  22.             if (!source_file.exists() || !source_file.isFile())
  23.                 throw new FileCopyException("FileCopy: no such source file: " +
  24.                                 source_name);
  25.             if (!source_file.canRead())
  26.                 throw new FileCopyException("FileCopy: source file " + 
  27.                                 "is unreadable: " + source_name);
  28.             
  29.             // If the destination exists, make sure it is a writeable file
  30.             // and ask before overwriting it.  If the destination doesn't
  31.             // exist, make sure the directory exists and is writeable.
  32.             if (destination_file.exists()) {
  33.                 if (destination_file.isFile()) {
  34.                     DataInputStream in = new DataInputStream(System.in);
  35.                     String response;
  36.                     
  37.                     if (!destination_file.canWrite())
  38.                         throw new FileCopyException("FileCopy: destination " +
  39.                                         "file is unwriteable: " + dest_name);
  40.                     
  41.                     System.out.print("File " + dest_name + 
  42.                              " already exists.  Overwrite? (Y/N): ");
  43.                     System.out.flush();
  44.                     response = in.readLine();
  45.                     if (!response.equals("Y") && !response.equals("y"))
  46.                         throw new FileCopyException("FileCopy: copy cancelled.");
  47.                 }
  48.                 else
  49.                     throw new FileCopyException("FileCopy: destination "
  50.                                     + "is not a file: " +  dest_name);
  51.             }
  52.             else {
  53.                 File parentdir = parent(destination_file);
  54.                 if (!parentdir.exists())
  55.                     throw new FileCopyException("FileCopy: destination "
  56.                                     + "directory doesn't exist: " + dest_name);
  57.                 if (!parentdir.canWrite())
  58.                     throw new FileCopyException("FileCopy: destination "
  59.                                     + "directory is unwriteable: " + dest_name);
  60.             }
  61.             
  62.             // If we've gotten this far, then everything is okay; we can
  63.             // copy the file.
  64.             source = new FileInputStream(source_file);
  65.             destination = new FileOutputStream(destination_file);
  66.             buffer = new byte[1024];
  67.             while(true) {
  68.                 bytes_read = source.read(buffer);
  69.                 if (bytes_read == -1) break;
  70.                 destination.write(buffer, 0, bytes_read);
  71.             }
  72.         }
  73.         // No matter what happens, always close any streams we've opened.
  74.         finally {
  75.             if (source != null) 
  76.                 try source.close(); catch (IOException e) ;
  77.             if (destination != null) 
  78.                 try destination.close(); catch (IOException e) ;
  79.         }
  80.     }
  81.     
  82.     // File.getParent() can return null when the file is specified without
  83.     // a directory or is in the root directory.  
  84.     // This method handles those cases.
  85.     private static File parent(File f) {
  86.         String dirname = f.getParent();
  87.         if (dirname == null) {
  88.             if (f.isAbsolute()) return new File(File.separator);
  89.             else return new File(System.getProperty("user.dir"));
  90.         }
  91.         return new File(dirname);
  92.     }
  93.     
  94.     public static void main(String[] args) {
  95.         if (args.length != 2)
  96.             System.err.println("Usage: java FileCopy " + 
  97.                        "<source file> <destination file>");
  98.         else {
  99.             try copy(args[0], args[1]);
  100.             catch (IOException e) System.err.println(e.getMessage());
  101.         }
  102.     }
  103. }
  104.  
  105. class FileCopyException extends IOException { 
  106.     public FileCopyException(String msg) { super(msg); }
  107. }
  108.